package com.trkj.service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.trkj.dao.jpa.IHwcShiftDAO;
import com.trkj.pojos.Tbshift;

/**
 * 班次业务类
 * @author lenovo
 *
 */
@Service
@Transactional
public class HwcShiftService {
	
	@Autowired
	private IHwcShiftDAO shiftDAO;
	
	/**
	 * 添加sihft方法
	 * @param shiftName
	 * @param startDate
	 * @param endDate
	 */
	public void addShift(String shiftName,String startDate,String endDate){
			
			Tbshift shift = new Tbshift();
			shift.setShiftName(shiftName);
			shift.setStartDate(startDate);
			shift.setEndDate(endDate);
			shift.setShiftState("启用");
			shiftDAO.save(shift);
	}
	
	/**
	 * 查询所有没有被废弃的班次
	 * @return
	 */
	public Map<String,Object> queryShift(int page,int rows){
		PageRequest pr = new PageRequest(page -1,rows);
		//获取当页记录
		
		List<Tbshift>list= shiftDAO.queryShift(pr);
		
		//获取总的记录数
		Map<String,Object> map = new HashMap<>();
		map.put("total", shiftDAO.queryShiftCount());
		map.put("rows", list);
		return map;
	}
	/**
	 * 返回所有没有被废弃的SHIft（不分页）
	 * @return
	 */
	public List<Tbshift> queryAllShift(){
		return shiftDAO.queryAllShift();
	}
	/**
	 * 修改班次
	 * @param shift
	 */
	public void updateShift(Tbshift shift){
		Tbshift st = shiftDAO.findOne(shift.getShiftNo());
		st.setShiftName(shift.getShiftName());
		st.setStartDate(shift.getStartDate());
		st.setEndDate(shift.getEndDate());
		st.setShiftState(shift.getShiftState());
		shiftDAO.save(st);
	}
	
	/**
	 * 根据班次编号查询班次
	 * @param shiftNo
	 * @return
	 */
	public Tbshift findShiftNoByShift(long shiftNo){
		return shiftDAO.findOne(shiftNo);
	}
	/**
	 * 删除班次
	 * @param shiftNos
	 */
	public void delShift(Long[] shiftNos){
		//将数组转换成集合
		DefaultConversionService service =  new DefaultConversionService();
		List<Long> list = service.convert(shiftNos, List.class);
		for(Long l :list){
			Tbshift shift = shiftDAO.findOne(l);
			shift.setShiftState("废弃");
			shiftDAO.save(shift);
		}

	}
	
	
}
